#include "base/core/os/directory.h"
#include <shlwapi.h>
#include <direct.h>
#include <tchar.h>
#include <string>
#include "base/core/com_define.h"

bool PathIsExist(const char * path) 
{
	MMO_ASSERT(path);
	if (path)
		return (PathFileExists(path) != FALSE);
	else
		return false;
}

bool MakeDirectory(const char * path, bool is_hide/* = false*/) 
{
	if (!path)
		return false;

	if (PathFileExists(path))
		return true;

	std::string directory_path(path);
	std::string::size_type segment_offset = directory_path.rfind(MMO_PATH_SEPRATOR);

	if (segment_offset != directory_path.npos) 
	{
		std::string parent_path = directory_path.substr(0, segment_offset);
		if (!PathFileExists(parent_path.c_str())
			&& !MakeDirectory(parent_path.c_str(), is_hide)) 
		{
			return false;
		}
	}

	if (!PathFileExists(path) && _mkdir(directory_path.c_str()) != 0) 
	{
		MMO_ASSERT(false);
		return false;
	}

	if (is_hide)
		SetFileAttributes(_T(directory_path.c_str()),
						(FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM));
	return true;
}

std::string WorkDiretory() 
{
	char process_file[MAX_PATH];
	int  position = GetModuleFileName(NULL, process_file, sizeof(process_file));
	if (position == 0) return std::string("");

	while ((position > 0) && process_file[position] != MMO_PATH_SEPRATOR)
	{
		--position;
	}
	process_file[position] = '\0';
	return std::string(process_file);
}

